package mop.gui.task;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import mop.controller.RemoveTaskController;
import mop.gui.GUI;
import mop.model.task.TaskInterface;

/**
 * RemoveTaskAction
 * 
 * @author  Laurens
 */
public class RemoveTaskAction implements MouseListener 
{
    private GUI gui;
    private RemoveTaskController removeTaskController;
    private TaskInterface task;
    
    /**
     * Constructor.
     * 
     * @param   gui
     * @param   removeTaskController
     * @param   task
     */
    public RemoveTaskAction(GUI gui, RemoveTaskController removeTaskController, TaskInterface task)
    {
        this.gui = gui;
        this.removeTaskController = removeTaskController;
        this.task = task;
    }

    public void mouseClicked(MouseEvent e)
    {
        int result = JOptionPane.showConfirmDialog(null, 
                "Are you sure you want to remove this task?", "Are you sure?", JOptionPane.YES_NO_OPTION);
        
        // Continue with use case?
        if (result == JOptionPane.YES_OPTION)
        {
            // Check for dependent tasks.
            boolean canRemove;
            try {
                canRemove = removeTaskController.canBeRemoved(task.getId());
            } catch (Throwable exception) {
                gui.handleError(exception);
                return;
            }
            
            if (canRemove)
            {
                // Basic flow.
                try {
                    removeTaskController.removeTask(task.getId(), false);
                } catch (Throwable exception) {
                    gui.handleError(exception);
                    return;
                }
            }
            else
            {
                // Alternate flow.
                LinkedList<TaskInterface> parents = new LinkedList<TaskInterface>();
                
                try {
                    parents = (LinkedList<TaskInterface>) task.getAllParentList();
                } catch (Throwable exception) {
                    gui.handleError(exception);
                    return;
                }
                
                result = JOptionPane.showConfirmDialog(null, 
                    "The following tasks depend on this task:\n\n" + printTasks(parents) + 
                    "\nDo you want to remove all (recursively) dependent tasks?", 
                    "Dependent tasks detected!", JOptionPane.YES_NO_CANCEL_OPTION);
                
                if (result == JOptionPane.YES_OPTION)
                {
                    try {
                        removeTaskController.removeTask(task.getId(), true);
                    } catch (Throwable exception) {
                        gui.handleError(exception);
                        return;
                    }
                }
                else if (result == JOptionPane.NO_OPTION)
                {
                    try {
                        removeTaskController.removeTask(task.getId(), false);
                    } catch (Throwable exception) {
                        gui.handleError(exception);
                        return;
                    }
                }
            }
            
            gui.handleSuccess("Task removed!");
            gui.getMainWindow().update();  // unchecked typecast, but should be okay
        }
    }
    
    /**
     * Prints a list of tasks (one task per line).
     * 
     * @param   tasks
     * @return  A list of tasks (one task per line).
     */
    private String printTasks(LinkedList<TaskInterface> tasks)
    {
        StringBuffer sb = new StringBuffer();
        for(TaskInterface t : tasks)
            sb.append(t.getId() + "\n");
        return sb.toString();
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {        
    }

    public void mouseExited(MouseEvent e)
    {
    }
}